package com.zmj.sy.mom.srv.aps.service.scheduler.resource;

import com.zmj.sy.mom.srv.aps.bean.entity.resource.Group;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.Phase;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.Station;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Getter
@Setter
public class DefaultFactory {

    /**
     * 工作组信息
     */
    private List<Group> groups;

    private List<Station> stations;

    private List<Phase> phases;

    private transient Map<String, Group> groupMap;

    private transient List<Integer> groupIds;

    private transient Map<Integer, List<Station>> groupStationMap;

    private transient Map<Integer, Station> stationMap;

    private transient Map<String, Phase> phaseMap;

    public List<Integer> groupIds() {
        if (groupIds == null) {
            groupIds = groups.stream().map(Group::getId).collect(Collectors.toList());
        }
        return groupIds;
    }

    public Group group(String myCode) {
        if (groupMap == null) {
            groupMap = groups.stream()
                    .collect(Collectors.toMap(Group::getMyCode, Function.identity(), (e1, e2) -> e2));
        }
        return groupMap.get(myCode);
    }

    public List<Station> stations(Integer groupId) {
        if (groupStationMap == null) {
            groupStationMap = stations.stream()
                    .collect(Collectors.groupingBy(Station::getGroupId));
        }
        return groupStationMap.getOrDefault(groupId, new ArrayList<>());
    }

    public Station station(Integer stationId) {
        if (stationMap == null) {
            stationMap = stations.stream()
                    .collect(Collectors.toMap(Station::getId, Function.identity()));
        }
        return stationMap.get(stationId);
    }

    public Phase phase(String phaseCode) {
        if (phaseMap == null) {
            phaseMap = phases.stream()
                    .collect(Collectors.toMap(Phase::getMyCode, Function.identity()));
        }
        return phaseMap.get(phaseCode);
    }

}
